package com.first.homeqian.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.first.common.pojo.TbBottomArticle;
import com.first.homeqian.mapper.BottomArticleMapper;
import com.first.homeqian.service.BottomArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: finrsteducation-parent
 * @description:
 * @author: 张玉森
 * @create: 2023-06-02 10:35
 **/
@Service
public class BottomArticleServiceImpl implements BottomArticleService {
    @Autowired
    private BottomArticleMapper qianBottomArticleMapper;
    @Override
    public List<TbBottomArticle> AllBottomArticle() {

        QueryWrapper<TbBottomArticle> wrapper = new QueryWrapper<>();
        ////没有被删除的父目录
        wrapper.eq("is_deleted", 0);
        wrapper.eq("parent_id", 0);
        wrapper.eq("is_disable",0);
        List<TbBottomArticle> tbBottomArticles = qianBottomArticleMapper.selectList(wrapper);

        QueryWrapper<TbBottomArticle> childrenWrapper = new QueryWrapper<>();
        childrenWrapper.eq("is_deleted", 0);
        childrenWrapper.eq("is_disable",0);
        List<TbBottomArticle> list = qianBottomArticleMapper.selectList(childrenWrapper);
        
        //遍历records集合，设置孩子
        for (TbBottomArticle item : tbBottomArticles) {
            List<TbBottomArticle> children = getChildren(item, list);
            item.setChildren(children);
        }
        return tbBottomArticles;
    }

    private List<TbBottomArticle> getChildren(TbBottomArticle item, List<TbBottomArticle> childrens) {
        ArrayList<TbBottomArticle> list = new ArrayList<>();
        for (TbBottomArticle children : childrens) {
            if (item.getId().equals(children.getParentId())) {
                list.add(children);
            }
        }
        for (TbBottomArticle a : list) {
            a.setChildren(getChildren(a, childrens));
        }
        return list;
    }
}
